﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;

namespace HslCommunication.Core.Net
{
	/// <summary>
	/// 异形客户端的基类，提供了基础的异形操作<br />
	/// The base class of the profiled client provides the basic profiled operation
	/// </summary>
	// Token: 0x02000135 RID: 309
	public class NetworkAlienClient : NetworkServerBase
	{
		/// <summary>
		/// 默认的无参构造方法<br />
		/// The default parameterless constructor
		/// </summary>
		// Token: 0x060019CE RID: 6606 RVA: 0x00080FA4 File Offset: 0x0007F1A4
		public NetworkAlienClient()
		{
			this.password = new byte[6];
			this.trustOnline = new List<string>();
			this.trustLock = new SimpleHybirdLock();
		}

		/// <summary>
		/// 当接收到了新的请求的时候执行的操作<br />
		/// An action performed when a new request is received
		/// </summary>
		/// <param name="socket">异步对象</param>
		/// <param name="endPoint">终结点</param>
		// Token: 0x060019CF RID: 6607 RVA: 0x00080FF0 File Offset: 0x0007F1F0
		[DebuggerStepThrough]
		protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
		{
			NetworkAlienClient.<ThreadPoolLogin>d__1 <ThreadPoolLogin>d__ = new NetworkAlienClient.<ThreadPoolLogin>d__1();
			<ThreadPoolLogin>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<ThreadPoolLogin>d__.<>4__this = this;
			<ThreadPoolLogin>d__.socket = socket;
			<ThreadPoolLogin>d__.endPoint = endPoint;
			<ThreadPoolLogin>d__.<>1__state = -1;
			<ThreadPoolLogin>d__.<>t__builder.Start<NetworkAlienClient.<ThreadPoolLogin>d__1>(ref <ThreadPoolLogin>d__);
		}

		/// <summary>
		/// 是否返回响应，默认为 <c>True</c><br />
		/// The default is <c>True</c>
		/// </summary>
		// Token: 0x1700055F RID: 1375
		// (get) Token: 0x060019D0 RID: 6608 RVA: 0x00081037 File Offset: 0x0007F237
		// (set) Token: 0x060019D1 RID: 6609 RVA: 0x0008103F File Offset: 0x0007F23F
		public bool IsResponseAck
		{
			get
			{
				return this.isResponseAck;
			}
			set
			{
				this.isResponseAck = value;
			}
		}

		/// <summary>
		/// 是否统一检查密码，如果每个会话需要自己检查密码，就需要设置为false<br />
		/// Whether to check the password uniformly, if each session needs to check the password by itself, it needs to be set to false
		/// </summary>
		// Token: 0x17000560 RID: 1376
		// (get) Token: 0x060019D2 RID: 6610 RVA: 0x00081048 File Offset: 0x0007F248
		// (set) Token: 0x060019D3 RID: 6611 RVA: 0x00081050 File Offset: 0x0007F250
		public bool IsCheckPwd
		{
			get
			{
				return this.isCheckPwd;
			}
			set
			{
				this.isCheckPwd = value;
			}
		}

		/// <summary>
		/// 当有服务器连接上来的时候触发<br />
		/// Triggered when a server is connected
		/// </summary>
		// Token: 0x1400003A RID: 58
		// (add) Token: 0x060019D4 RID: 6612 RVA: 0x0008105C File Offset: 0x0007F25C
		// (remove) Token: 0x060019D5 RID: 6613 RVA: 0x00081094 File Offset: 0x0007F294
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event NetworkAlienClient.OnClientConnectedDelegate OnClientConnected = null;

		/// <summary>
		/// 获取返回的命令信息
		/// </summary>
		/// <param name="status">状态</param>
		/// <returns>回发的指令信息</returns>
		// Token: 0x060019D6 RID: 6614 RVA: 0x000810C9 File Offset: 0x0007F2C9
		private byte[] GetResponse(byte status)
		{
			return new byte[]
			{
				72,
				115,
				110,
				0,
				1,
				0
			};
		}

		/// <summary>
		/// 检测当前的DTU是否在线
		/// </summary>
		/// <param name="session">当前的会话信息</param>
		/// <returns>当前的会话是否在线</returns>
		// Token: 0x060019D7 RID: 6615 RVA: 0x00047BE1 File Offset: 0x00045DE1
		public virtual int IsClientOnline(AlienSession session)
		{
			return 0;
		}

		/// <summary>
		/// 检测当前的dtu是否允许登录
		/// </summary>
		/// <param name="session">当前的会话信息</param>
		/// <returns>当前的id是否可允许登录</returns>
		// Token: 0x060019D8 RID: 6616 RVA: 0x000810E0 File Offset: 0x0007F2E0
		private bool IsClientPermission(AlienSession session)
		{
			bool result = false;
			this.trustLock.Enter();
			bool flag = this.trustOnline.Count == 0;
			if (flag)
			{
				result = true;
			}
			else
			{
				for (int i = 0; i < this.trustOnline.Count; i++)
				{
					bool flag2 = this.trustOnline[i] == session.DTU;
					if (flag2)
					{
						result = true;
						break;
					}
				}
			}
			this.trustLock.Leave();
			return result;
		}

		/// <summary>
		/// 设置密码，需要传入长度为6的字节数组<br />
		/// To set the password, you need to pass in an array of bytes of length 6
		/// </summary>
		/// <param name="password">密码信息</param>
		// Token: 0x060019D9 RID: 6617 RVA: 0x00081168 File Offset: 0x0007F368
		public void SetPassword(byte[] password)
		{
			bool flag = password != null && password.Length == 6;
			if (flag)
			{
				password.CopyTo(this.password, 0);
			}
		}

		/// <summary>
		/// 设置可信任的客户端列表，传入一个DTU的列表信息<br />
		/// Set up the list of trusted clients, passing in the list information for a DTU
		/// </summary>
		/// <param name="clients">客户端列表</param>
		// Token: 0x060019DA RID: 6618 RVA: 0x00081196 File Offset: 0x0007F396
		public void SetTrustClients(string[] clients)
		{
			this.trustOnline = new List<string>(clients);
		}

		/// <inheritdoc />
		// Token: 0x060019DB RID: 6619 RVA: 0x000811A5 File Offset: 0x0007F3A5
		public override string ToString()
		{
			return "NetworkAlienBase";
		}

		/// <summary>
		/// 获取错误的描述信息
		/// </summary>
		/// <param name="dtu">dtu信息</param>
		/// <param name="code">错误码</param>
		/// <returns>错误信息</returns>
		// Token: 0x060019DC RID: 6620 RVA: 0x000811AC File Offset: 0x0007F3AC
		public static string GetMsgFromCode(string dtu, int code)
		{
			bool flag = code == 0;
			string result;
			if (flag)
			{
				result = "Login Success, Id:" + dtu;
			}
			else
			{
				bool flag2 = code == 1;
				if (flag2)
				{
					result = "Login Repeat, Id:" + dtu;
				}
				else
				{
					bool flag3 = code == 2;
					if (flag3)
					{
						result = "Login Forbidden, Id:" + dtu;
					}
					else
					{
						bool flag4 = code == 3;
						if (flag4)
						{
							result = "Login Passwod Wrong, Id:" + dtu;
						}
						else
						{
							result = "Login Unknow reason, Id:" + dtu;
						}
					}
				}
			}
			return result;
		}

		// Token: 0x040005D0 RID: 1488
		private byte[] password;

		// Token: 0x040005D1 RID: 1489
		private List<string> trustOnline;

		// Token: 0x040005D2 RID: 1490
		private SimpleHybirdLock trustLock;

		// Token: 0x040005D3 RID: 1491
		private bool isResponseAck = true;

		// Token: 0x040005D4 RID: 1492
		private bool isCheckPwd = true;

		/// <summary>
		/// 状态登录成功
		/// </summary>
		// Token: 0x040005D5 RID: 1493
		public const byte StatusOk = 0;

		/// <summary>
		/// 重复登录
		/// </summary>
		// Token: 0x040005D6 RID: 1494
		public const byte StatusLoginRepeat = 1;

		/// <summary>
		/// 禁止登录
		/// </summary>
		// Token: 0x040005D7 RID: 1495
		public const byte StatusLoginForbidden = 2;

		/// <summary>
		/// 密码错误
		/// </summary>
		// Token: 0x040005D8 RID: 1496
		public const byte StatusPasswodWrong = 3;

		/// <summary>
		/// 客户上线的委托事件
		/// </summary>
		/// <param name="session">异形客户端的会话信息</param>
		// Token: 0x0200074C RID: 1868
		// (Invoke) Token: 0x0600318C RID: 12684
		public delegate void OnClientConnectedDelegate(AlienSession session);
	}
}
